<template>
  <div>
    <el-button type="primary" @click="visible = true">新增</el-button>
    <el-dialog title="新增" :visible.sync="visible">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="诗词名：" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="诗词内容：" prop="content">
          <el-input type="textarea" rows="8" v-model="ruleForm.content"></el-input>
        </el-form-item>
        <el-form-item label="标签：" prop="tags">
          <el-input v-model="ruleForm.tags" placeholder="多个标签以“，”分开"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getAuthorCreateWorks } from '@/api/author'

export default {
  name: 'create',
  props: {
    authorInfo: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      visible: false,
      ruleForm: {
        title: '',
        content: '',
        tags: ''
      },
      rules: {
        title: [{ required: true, message: '请输入诗词名', trigger: 'blur' }],
        content: [{ required: true, message: '请输入诗词内容', trigger: 'blur' }],
        tags: [{ required: true, message: '请输入标签', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleSubmit() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          let { name, dynasty } = this.authorInfo
          let res = await getAuthorCreateWorks({ ...this.ruleForm, name, dynasty })
          let { code, msg } = res
          if (code === 0) {
            this.$message.success(msg)
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
// @import './index.scss';
</style>
